package baser

import (
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/core/common"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd"
	"gogs.xiaoyuanjijiehao.com/antlinker/antcms/ddd/repo/mgorepo"

	"github.com/globalsign/mgo/bson"
)

// BaseRepo 基础封装
type BaseRepo struct {
	mgorepo.BaseRepo
}

// DBQueryPage 分页查询
func (r BaseRepo) DBQueryPage(ctx ddd.Context, collname string, pi common.PageInfo, pageidname string, query bson.M, field interface{}, sort []string, result interface{}) (pr1 common.PageResult, err error) {
	pi1 := mgorepo.PageInfo{
		PageSize: pi.PageSize,

		Direct:  pi.Direct,
		Desc:    pi.Desc,
		Current: pi.Current,
		Mode:    int(pi.Mode),
	}
	if pi.PageID != "" {
		pi1.PageID = pi.PageID
	}
	pr, err := r.BaseRepo.DBQueryPage(ctx, collname, pi1, pageidname, query, field, sort, result)
	pr1 = common.PageResult{
		Current:  pr.Current,
		Total:    pr.Total,
		PageSize: pr.PageSize,
		End:      pr.End,
	}
	return
}

// JSONSchTime2Bson 将json时间搜索转换为Bson bson.M
func (r BaseRepo) JSONSchTime2Bson(st common.SchTime) interface{} {
	ptime := bson.M{}
	if !st.Eq.IsZore() {
		return st.Eq.ToTime()
	}
	if !st.Start.IsZore() {
		ptime["$gt"] = st.Start.ToTime()
	}
	if !st.End.IsZore() {
		ptime["$lte"] = st.End.ToTime()
	}
	if len(ptime) > 0 {
		return ptime
	}
	return nil
}
